package cn.edu.seu.herald.informationplatform.util.filetools;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.imageio.ImageIO;

import cn.edu.seu.herald.informationplatform.bean.league.Activity;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class PicTools {
	public static void copy(String src, String dest) throws IOException {
		byte[] buffer = new byte[1024];
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			in = new FileInputStream(src);
			out = new FileOutputStream(dest);
			int c;
			while((c = in.read(buffer)) >= 0) {
				out.write(buffer, 0, c);
			}
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (Exception err) {
					// Ignore the exception.
				}
			}
			if (out != null) {
				try {
				out.close();
				} catch (Exception err) {
					//Ignore the exception.
				}
			}
		}
	}
	public static void DeletePic(String preRealPath){
		if(preRealPath!=null){
		File prefile = null;
		Properties pro = System.getProperties();
        String osName = pro.getProperty("os.name");
	     if("linux".equalsIgnoreCase(osName)){
	    	 prefile = new File(preRealPath);  
	     }else{
	    	 prefile = new File(preRealPath.replaceAll("\\\\", "/"));
	     }
	     if (prefile.isFile() && prefile.exists()) {  
	    	 prefile.delete();  
		 }
		}
	}
	
	public static void DeletePicFromReal(String savePath){
		if(savePath!=null){
		File prefile = new File(savePath);  
	     if (prefile.isFile() && prefile.exists()) {  
	    	 prefile.delete();  
		 }
		}
	}
	public static void correctPicSize(String savePath)
			{try{
				File saveFile = new File(savePath);
				Image src = ImageIO.read(saveFile);             
				double old_w=src.getWidth(null);                                     
				double old_h=src.getHeight(null);   
				double new_w=0;
				double new_h=0;
				double w_max=900;
				double h_max=550;		
				double w_h_rate = old_w/old_h;
				double w_h_rate_max = w_max/h_max;
				double tempdouble; 
				if(w_h_rate>w_h_rate_max&&old_w>w_max){
				tempdouble=w_max/old_w;
				new_w=w_max;
				new_h=old_h*tempdouble;
				DeletePicFromReal(savePath);
				}else if(w_h_rate<=w_h_rate_max&&old_h>h_max){
				tempdouble=h_max/old_h;
				new_h=h_max;
				new_w=tempdouble*old_w;
				DeletePicFromReal(savePath);
				}else {
					new_w=old_w;
					new_h=old_h;
				}
				BufferedImage tag = new BufferedImage((int)new_w,(int)new_h,BufferedImage.TYPE_INT_RGB);
				tag.getGraphics().drawImage(src,0,0,(int)new_w,(int)new_h,null); 
				
				FileOutputStream newimage=new FileOutputStream(savePath);          //输出到文件流
				
				JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);       
				encoder.encode(tag);                                               //近JPEG编码
				newimage.close();
				
				}catch (Exception e) {
					e.printStackTrace();
					
				}
			
		}
}
